﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
using System.Collections.Generic;
namespace ElingOA6_DSJCJ.DAL
{
	/// <summary>
	/// 数据访问类:PaperAnswer
	/// </summary>
    public partial class PaperAnswer
    {
        public PaperAnswer()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(decimal FPaperTopicId, decimal FPaperAnswerManagerID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_PaperAnswer");
            strSql.Append(" where FPaperTopicId=@FPaperTopicId and FPaperAnswerManagerID=@FPaperAnswerManagerID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = FPaperTopicId;
            parameters[1].Value = FPaperAnswerManagerID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(ElingOA6_DSJCJ.Model.PaperAnswer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_PaperAnswer(");
            strSql.Append("FPaperTopicId,FAnswer,FIsCorrect,FWriteAnswer,FPaperAnswerManagerID)");
            strSql.Append(" values (");
            strSql.Append("@FPaperTopicId,@FAnswer,@FIsCorrect,@FWriteAnswer,@FPaperAnswerManagerID)");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FAnswer", SqlDbType.VarChar,6),
					new SqlParameter("@FIsCorrect", SqlDbType.Decimal,5),
					new SqlParameter("@FWriteAnswer", SqlDbType.NVarChar),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = model.FPaperTopicId;
            parameters[1].Value = model.FAnswer;
            parameters[2].Value = model.FIsCorrect;
            parameters[3].Value = model.FWriteAnswer;
            parameters[4].Value = model.FPaperAnswerManagerID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public void AddItem(Model.PaperAnswerManager paperAnswerManagerModel, List<Model.PaperAnswer> paperAnswerList)
        {
            List<CommandInfo> commandInfoList = new List<CommandInfo>();
            CommandInfo commandInfo = new CommandInfo();
            StringBuilder strSql = new StringBuilder();
            SqlParameter[] parameters = new SqlParameter[] { };

            #region 回答答案主表
            strSql = new StringBuilder();
            strSql.Append("insert into t_PaperAnswerManager(");
            strSql.Append("FPaperId,FEntityNo,FEmplNo,FDeptNo,FAnswerDate,FIp,FSurverName,FSurverContact,FSurverUnitName,FSurverAddress,FSurverPostCode,FSurverEMail)");
            strSql.Append(" values (");
            strSql.Append("@FPaperId,@FEntityNo,@FEmplNo,@FDeptNo,@FAnswerDate,@FIp,@FSurverName,@FSurverContact,@FSurverUnitName,@FSurverAddress,@FSurverPostCode,@FSurverEMail)");
            strSql.Append(";SELECT @FPaperAnswerManagerID=IDENT_CURRENT('t_PaperAnswerManager')");
            parameters = new SqlParameter[] {
					new SqlParameter("@FPaperId", SqlDbType.Decimal,9),
					new SqlParameter("@FEntityNo", SqlDbType.NVarChar,255),
					new SqlParameter("@FEmplNo", SqlDbType.VarChar,30),
					new SqlParameter("@FDeptNo", SqlDbType.VarChar,30),
					new SqlParameter("@FAnswerDate", SqlDbType.VarChar,30),
					new SqlParameter("@FIp", SqlDbType.VarChar,15),
					new SqlParameter("@FSurverName", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverContact", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverUnitName", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverAddress", SqlDbType.NVarChar,60),
					new SqlParameter("@FSurverPostCode", SqlDbType.NVarChar,30),
					new SqlParameter("@FSurverEMail", SqlDbType.NVarChar,30),
            new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = paperAnswerManagerModel.FPaperId;
            parameters[1].Value = paperAnswerManagerModel.FEntityNo;
            parameters[2].Value = paperAnswerManagerModel.FEmplNo;
            parameters[3].Value = paperAnswerManagerModel.FDeptNo;
            parameters[4].Value = paperAnswerManagerModel.FAnswerDate;
            parameters[5].Value = paperAnswerManagerModel.FIp;
            parameters[6].Value = paperAnswerManagerModel.FSurverName;
            parameters[7].Value = paperAnswerManagerModel.FSurverContact;
            parameters[8].Value = paperAnswerManagerModel.FSurverUnitName;
            parameters[9].Value = paperAnswerManagerModel.FSurverAddress;
            parameters[10].Value = paperAnswerManagerModel.FSurverPostCode;
            parameters[11].Value = paperAnswerManagerModel.FSurverEMail;
            parameters[12].Direction = ParameterDirection.Output;
            commandInfo = new CommandInfo(strSql.ToString(), parameters);
            commandInfoList.Add(commandInfo);
            #endregion

            #region
            if (paperAnswerList != null)
            {
                foreach (Model.PaperAnswer model in paperAnswerList)
                {
                    string fiscorrect = "select COUNT(0) from t_PaperTopic where FPaperTopicId=" + model.FPaperTopicId + " and FStdAnswer='" + model.FAnswer + "'";
                    model.FIsCorrect = (int)DbHelperSQL.GetSingle(fiscorrect) == 0 ? 0 : 1;
                    strSql = new StringBuilder();
                    strSql.Append("insert into t_PaperAnswer(");
                    strSql.Append("FPaperTopicId,FAnswer,FIsCorrect,FWriteAnswer,FPaperAnswerManagerID)");
                    strSql.Append(" values (");
                    strSql.Append("@FPaperTopicId,@FAnswer,@FIsCorrect,@FWriteAnswer,@FPaperAnswerManagerID)");
                    parameters = new SqlParameter[] {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FAnswer", SqlDbType.VarChar,6),
					new SqlParameter("@FIsCorrect", SqlDbType.Decimal,5),
					new SqlParameter("@FWriteAnswer", SqlDbType.NVarChar),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
                    parameters[0].Value = model.FPaperTopicId;
                    parameters[1].Value = model.FAnswer;
                    parameters[2].Value = model.FIsCorrect;
                    parameters[3].Value = model.FWriteAnswer;
                    parameters[4].Direction = ParameterDirection.InputOutput;
                    commandInfo = new CommandInfo(strSql.ToString(), parameters);
                    commandInfoList.Add(commandInfo);
                }
            }
            #endregion

            DbHelperSQL.ExecuteSqlTranWithCommonList(commandInfoList);

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ElingOA6_DSJCJ.Model.PaperAnswer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_PaperAnswer set ");
            strSql.Append("FAnswer=@FAnswer,");
            strSql.Append("FIsCorrect=@FIsCorrect,");
            strSql.Append("FWriteAnswer=@FWriteAnswer");
            strSql.Append(" where FPaperTopicId=@FPaperTopicId and FPaperAnswerManagerID=@FPaperAnswerManagerID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FAnswer", SqlDbType.VarChar,6),
					new SqlParameter("@FIsCorrect", SqlDbType.Decimal,5),
					new SqlParameter("@FWriteAnswer", SqlDbType.NVarChar),
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = model.FAnswer;
            parameters[1].Value = model.FIsCorrect;
            parameters[2].Value = model.FWriteAnswer;
            parameters[3].Value = model.FPaperTopicId;
            parameters[4].Value = model.FPaperAnswerManagerID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(decimal FPaperTopicId, decimal FPaperAnswerManagerID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_PaperAnswer ");
            strSql.Append(" where FPaperTopicId=@FPaperTopicId and FPaperAnswerManagerID=@FPaperAnswerManagerID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = FPaperTopicId;
            parameters[1].Value = FPaperAnswerManagerID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ElingOA6_DSJCJ.Model.PaperAnswer GetModel(decimal FPaperTopicId, decimal FPaperAnswerManagerID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 FPaperTopicId,FAnswer,FIsCorrect,FWriteAnswer,FPaperAnswerManagerID from t_PaperAnswer ");
            strSql.Append(" where FPaperTopicId=@FPaperTopicId and FPaperAnswerManagerID=@FPaperAnswerManagerID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FPaperTopicId", SqlDbType.Decimal,9),
					new SqlParameter("@FPaperAnswerManagerID", SqlDbType.Decimal,9)};
            parameters[0].Value = FPaperTopicId;
            parameters[1].Value = FPaperAnswerManagerID;

            ElingOA6_DSJCJ.Model.PaperAnswer model = new ElingOA6_DSJCJ.Model.PaperAnswer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FPaperTopicId"] != null && ds.Tables[0].Rows[0]["FPaperTopicId"].ToString() != "")
                {
                    model.FPaperTopicId = decimal.Parse(ds.Tables[0].Rows[0]["FPaperTopicId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FAnswer"] != null && ds.Tables[0].Rows[0]["FAnswer"].ToString() != "")
                {
                    model.FAnswer = ds.Tables[0].Rows[0]["FAnswer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FIsCorrect"] != null && ds.Tables[0].Rows[0]["FIsCorrect"].ToString() != "")
                {
                    model.FIsCorrect = decimal.Parse(ds.Tables[0].Rows[0]["FIsCorrect"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FWriteAnswer"] != null && ds.Tables[0].Rows[0]["FWriteAnswer"].ToString() != "")
                {
                    model.FWriteAnswer = ds.Tables[0].Rows[0]["FWriteAnswer"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FPaperAnswerManagerID"] != null && ds.Tables[0].Rows[0]["FPaperAnswerManagerID"].ToString() != "")
                {
                    model.FPaperAnswerManagerID = decimal.Parse(ds.Tables[0].Rows[0]["FPaperAnswerManagerID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FPaperTopicId,FAnswer,FIsCorrect,FWriteAnswer,FPaperAnswerManagerID ");
            strSql.Append(" FROM t_PaperAnswer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" FPaperTopicId,FAnswer,FIsCorrect,FWriteAnswer,FPaperAnswerManagerID ");
            strSql.Append(" FROM t_PaperAnswer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_PaperAnswer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.FPaperAnswerManagerID desc");
            }
            strSql.Append(")AS Row, T.*  from t_PaperAnswer T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_PaperAnswer";
            parameters[1].Value = "FPaperAnswerManagerID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListGroupByAnswer(decimal FPaperTopicId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" declare @Table table(id int identity(1,1),sl char(1),aw char(1))");
            strSql.Append(" declare @Table2 table(FAnswer nvarchar(255),census int,aw char(1))");
            strSql.Append(" insert into @Table(sl,aw)");
            strSql.Append(" select FAnswerNo,FIsRestsAnswer from t_PaperAnswerOption where FPaperTopicId=" + FPaperTopicId + "");
            strSql.Append(" Declare @currentIndex int");
            strSql.Append(" Declare @totalRows int");
            strSql.Append(" declare @sl char(1) ");
            strSql.Append(" declare @aw char(1) ");
            strSql.Append(" declare @con int ");
            strSql.Append(" select @currentIndex=1 ");
            strSql.Append(" select @totalRows=count(1) from @Table ");
            strSql.Append(" while (@currentIndex<=@totalRows)");
            strSql.Append(" begin");
            strSql.Append(" select @sl=sl,@aw=aw from @Table where id=@currentIndex");
            strSql.Append(" select @con=COUNT(0) from t_PaperAnswer where FPaperTopicId=" + FPaperTopicId + " and FAnswer like '%'+@sl+'%'");
            strSql.Append(" if(@con<>0)");
            strSql.Append(" begin");
            strSql.Append(" insert into @Table2(FAnswer,census,aw)values(@sl,@con,@aw)");
            strSql.Append(" end");
            strSql.Append(" select @currentIndex=@currentIndex+1;  ");
            strSql.Append(" end");
            strSql.Append(" select * from @Table2");
            //strSql.Append(" select FWriteAnswer from t_PaperAnswer where FPaperTopicId=" + FPaperTopicId + " and FWriteAnswer<>''");
            //strSql.Append(" select FAnswer,COUNT(0) as census from t_PaperAnswer where FPaperTopicId=" + FPaperTopicId + "  group by FAnswer;
            //strSql.Append(" select FWriteAnswer from t_PaperAnswer where FPaperTopicId=" + FPaperTopicId + "");

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method
    }
}

